<template>
   <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @row-save="saveInsert"
      @row-update="updatePlace"
      :page="page"
      @on-load="onLoad">
    </avue-crud>
   </basic-container>
</template>

<script>
import {setStore, getStore} from '@/util/store'
import {getList,inserTemplate,updateTemplate} from '@/api/system/template'

export default{
    data(){
      return{
        userId:parseInt(getStore({name: 'userInfo'}).userId),
        roleId:parseInt(getStore({name: 'userInfo'}).roleId),   
        data:[],
        loading:false,
        page:{
          pageSize: 10,
          currentPage: 1,
          total: 1
        },
        option:{
              searchShow: true,
              searchMenuSpan: 6,
              tip: false,
              tree: true,
              border: true,
              selection: true,
              delBtn: false,
              addBtn:true,
              editBtn:true,
              menu:true,
              height:'auto',
              column:[
                {
                    label:"模板id",
                    prop:"template",
                },
                {
                    label:"详情备注",
                    prop:"remark",
                    search:true
                }
              ]
            },
      }
    },
    methods:{
      onLoad(page,params={}){
         getList(page.currentPage,page.pageSize,Object.assign(params, this.query)).then(res=>{
            if(res.data.code==200){
                this.data = res.data.data.data;
                this.page.total = res.data.data.count;
            }
         })
      },
      saveInsert(row,done,loading){
         if(row.template=='' || row.template==null || row.template==undefined){
            this.$message.error("请填写模板id");
            loading();
            return false;
         }
         inserTemplate(row).then(res=>{
            if(res.data.code==200){
                this.$message.success("新增成功");
                done();
                this.onLoad(this.page,{});
            }else{
              loading();
              this.$message.error(res.data.msg);
            }
         })
         
      },
      updatePlace(row,index,done,loading){
         console.log(row);
         if(row.template=='' || row.template==null || row.template==undefined){
            this.$message.error("请填写模板id");
            loading();
            return false;
         }
         updateTemplate(row).then(res=>{
            if(res.data.code==200){
                this.$message.success("修改成功");
                done();
                this.onLoad(this.page,{});
            }else{
              loading();
              this.$message.error(res.data.msg);
            }
         })
         
      },
      searchChange(params, done){
        console.log(params);
          this.page.currentPage = 1;
          this.onLoad(this.page, params);
          done();
      },
      searchReset(){
        this.parentId = 0;
        this.onLoad(this.page);
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
    }

}
</script>